package com.cgly.medical.entity;

import com.cgly.medical.domain.Account;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @program: medical
 * @description: 用于身份验证
 * @author: chen di
 * @create: 2022-08-15 15:05
 */

/*
Data 注解生成的 toString 方法也只包含了子类自有属性。
解决方案一样，加上 @ToString(callSuper = true) 注解，其实这里真正重要的是注解中的属性，callSuper = true，
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class AccountModel extends Account implements UserDetails {
    private Collection<? extends GrantedAuthority> authorities;



    /**
     * get方法由Spring Security调用，我们通过set方法或构造函数为 Spring Security提供UserDetails数据
     */
//    //实现UserDetails 需要加载 security 依赖




    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> list = new ArrayList<>();
        list.add(new GrantedAuthority() {
            @Override
            public String getAuthority() {
                switch (getUtype()) {
                    case "ROLE_1":
                        return "ADMIN";
                    default:
                        return "USER";
                }
            }
        });
        return list;
    }

    @Override
    public String getPassword() {
        return this.getPwd();
    }

    @Override
    public String getUsername() {
        return this.getUname();
    }

    /**
     * 账户是否过期
     **/
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 是否禁用
     */
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 密码是否过期
     */
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否启用
     */
    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }


}